<?php

namespace App\Http\Controllers\Api\V1;

use Carbon\Carbon;
use App\Models\User;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Api\V1\Resources\BoxCatrgroyResource;
use Rainsens\Dinglog\Facades\Dinglog;
use Illuminate\Support\Facades\DB;

class RanksController extends Controller
{
    public function index()
    {
        $source = request('source');


        
        if($source =='dividend'){
             $joinSql =  "(select sum(money) as money,user_id from apples where remark != 'get_apples' && remark !='gift_apples' && remark != 'apples_exchange_balances' && remark != 'apple_exchange_trees' && remark != 'apple_exchange_order' && remark != 'system_recall' group by user_id ) AS `r` ";
             $result =  User::query()->join(DB::Raw($joinSql),'r.user_id','=','users.id')->select('users.id','r.money','users.name')->orderByDesc('r.money')->take(100)->get()->toArray();
           
        }else{
             $joinSql =  "(select sum(money) as money,user_id from apples where remark != 'get_apples' && remark !='gift_apples' && remark != 'apples_exchange_balances' && remark != 'apple_exchange_trees' && remark != 'apple_exchange_order' && remark != 'system_recall' and created_at >= '".Carbon::yesterday()->toDateString()."' and created_at <= '".Carbon::today()->toDateString()."' group by user_id ) AS `r` ";
             $result =  User::query()->join(DB::Raw($joinSql),'r.user_id','=','users.id')->select('users.id','r.money','users.name')->orderByDesc('r.money')->take(100)->get()->toArray();
        }
    	
        return  response()->json(['ranks' => $result]);

    }

     public function config()
    {
        /**
         * @var User $user
         */
        $config=[
             'rank_banner'=>img_path(cs()->serve_qrcode),
              'rank_produce_image'=>img_path(cs()->produce_image)
        ]; 
         return response()->json($config);
    }

}
